package com.jstyle.jclifexd.view.sleepview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.jstyle.jclifexd.utils.ScreenUtils;


/**
 * 清醒
 */

public class SoberView extends View {
    float startX = 0;//开始距离
    float stopX = 0;//结束距离
    float linewith, lineheight;//0.5*6像素标志线  总高度大约 27dp
    float avgvalue;//当前最对10小时
    Context context;
    Paint back_ground;//背景画笔
    Paint back_groundcc;//背景画笔
    int fenzhong = 0;//共计多少分钟数据

    int greencolor = Color.parseColor("#FC7B3A");//标记颜色
    int defaultcolor = Color.parseColor("#999999");

    public SoberView(Context context) {
        super(context);
        init();
    }

    public SoberView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public SoberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        if (getWidth() > 0 && getHeight() > 0) {
            //绘制背景
            RectF baseRect = new RectF(startX, getHeight() - ScreenUtils.dip2px(context, 15f), (ScreenUtils.getScreenWidth(context) - stopX), getHeight());
            canvas.drawRoundRect(baseRect, ScreenUtils.dip2px(context, 9f), ScreenUtils.dip2px(context, 9f), back_ground);
            //绘制刻度线 20 总30分钟
            if (fenzhong != 0) {
                back_groundcc.setColor(greencolor);
                canvas.drawRoundRect(new RectF(startX, getHeight() - ScreenUtils.dip2px(context, 15f), startX + avgvalue * fenzhong + ScreenUtils.dip2px(context, 0.25f), getHeight()), ScreenUtils.dip2px(context, 9f), ScreenUtils.dip2px(context, 9f), back_groundcc);
            }
        }
        super.onDraw(canvas);
    }

    /**
     * 设置数据源
     */
    public void setDataSource(int myduration) {
        this.fenzhong = myduration>=600?600:myduration;
        invalidate();

    }


    private void init() {
        startX = ScreenUtils.dip2px(context, 20f);
        stopX = ScreenUtils.dip2px(context, 90f);
        linewith = ScreenUtils.dip2px(context, 0.5f);
        lineheight = ScreenUtils.dip2px(context, 6f);
        avgvalue = (ScreenUtils.getScreenWidth(context) - (startX + stopX)) / 600f;//总数30分钟

        back_ground = new Paint();
        back_ground.setColor(defaultcolor);
        back_ground.setDither(true); // 防抖动
        back_ground.setAntiAlias(true);// 抗锯齿

        back_groundcc = new Paint();
        back_groundcc.setColor(defaultcolor);
        back_groundcc.setDither(true); // 防抖动
        back_groundcc.setAntiAlias(true);// 抗锯齿



    }

    /**
     * 传入文字计算文字宽度
     *
     * @param paint
     * @param str
     * @return
     */
    private static int getTextWith(Paint paint, String str) {
        paint.measureText(str);
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        return rect.width();
    }

}
